package me.tonywang.security.authentication;

import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.util.Assert;

public class UserSignAuthenticationProvider implements AuthenticationProvider {

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        Assert.notNull(authentication, "No authentication data provided");
        String username = (String) authentication.getPrincipal();
        String sign = (String) authentication.getCredentials();
        //身份认证
        if (!(username.equals("admin")&&sign.equals("123"))) {
            throw new BadCredentialsException("Authentication Failed. Username or Password not valid.");
        }
        return new UserSignAuthenticationToken(authentication.getPrincipal(), authentication.getCredentials(),
                AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_ADMIN"));
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return (UserSignAuthenticationToken.class
                .isAssignableFrom(authentication));
    }

}
